package com.hanlink.dsp.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hanlink.dsp.util.Constants;
import com.qztech.sdk.R;

import static com.hanlink.dsp.util.Constants.DIENABLED_ALPHA;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015-11-16
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015-11-16        PeterVon       初始创建
 *
 * </pre>
 */
public class AdvanceChannel extends FrameLayout {

    private TextView mTxtValue;
    private VerticalSeekBar mVsbChanel;
    private ImageButton mIbtnPlus;
    private ImageButton mIbtnSub;

    private CheckBox mCbxMute;
    private DegreeCheckBox mCbxDegree;
    private long mLastChangedTs;
    private boolean isFromUser = true;

    private OnSeekBarCallbacks mOnSeekBarCallbacks;

    public AdvanceChannel(Context context) {
        super(context);
        initView(context);
    }

    public AdvanceChannel(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AdvanceChannel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }


    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_timedelay_channel_bar, this, true);
        mTxtValue = view.findViewById(R.id.txt_value);
        mIbtnPlus = view.findViewById(R.id.ibtn_plus);
        mIbtnSub = view.findViewById(R.id.ibtn_sub);
        mVsbChanel = view.findViewById(R.id.vsb_channel);

        mCbxMute = view.findViewById(R.id.txt_hz_value);
        mCbxDegree = view.findViewById(R.id.cb_180_value);

        mVsbChanel.setMax(Constants.MAX_VOL);
        mVsbChanel.setProgress(Constants.MAX_VOL);
        mVsbChanel.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {


            @SuppressLint("SetTextI18n")
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress == 0) {
                    mTxtValue.setText("Off");
                } else mTxtValue.setText((-Constants.MAX_VOL + progress) + "dB");
                if (mOnSeekBarCallbacks != null && isFromUser) {
                    if (System.currentTimeMillis() - mLastChangedTs > Constants.SEEK_BAR_DELAY) {
                        mLastChangedTs = System.currentTimeMillis();
                        mOnSeekBarCallbacks.onProgressChanged(seekBar, progress, (Integer) seekBar.getTag(), fromUser, true);
                    } else {
                        mOnSeekBarCallbacks.onProgressChanged(seekBar, progress, (Integer) seekBar.getTag(), fromUser, !fromUser);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seekBar.getProgress() == 0) {
                    mTxtValue.setText("Off");
                } else
                    mTxtValue.setText((-Constants.MAX_VOL + seekBar.getProgress()) + "dB");// - 20
                if (mOnSeekBarCallbacks != null && isFromUser) {
                    mOnSeekBarCallbacks.onProgressChangeOver(seekBar, seekBar.getProgress(), (Integer) seekBar.getTag());// - 20
                }
            }
        });

        mCbxMute.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (mOnSeekBarCallbacks != null && isFromUser) {
                mOnSeekBarCallbacks.onMuteChanged(buttonView, isChecked, (Integer) mCbxMute.getTag());
            }
            mVsbChanel.setEnabled(!isChecked);
            mIbtnPlus.setEnabled(!isChecked);
            mIbtnSub.setEnabled(!isChecked);
            mVsbChanel.setAlpha(isChecked? DIENABLED_ALPHA:1f);
            mIbtnPlus.setAlpha(isChecked? DIENABLED_ALPHA:1f);
            mIbtnSub.setAlpha(isChecked? DIENABLED_ALPHA:1f);
        });
        mCbxDegree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (mOnSeekBarCallbacks != null && isFromUser) {
                mOnSeekBarCallbacks.onDegreeChanged(buttonView, isChecked, (Integer) mCbxDegree.getTag());
            }
        });
        mIbtnSub.setOnClickListener(onClickListener);
        mIbtnPlus.setOnClickListener(onClickListener);
    }


    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.ibtn_plus) {
                if (mVsbChanel.getProgress() < mVsbChanel.getMax())
                    mVsbChanel.setProgress(mVsbChanel.getProgress() + 1);
            } else if (id == R.id.ibtn_sub) {
                if (mVsbChanel.getProgress() > 0)
                    mVsbChanel.setProgress(mVsbChanel.getProgress() - 1);
            }
        }
    };


    public void setEnabled(boolean enabled) {
        mVsbChanel.setEnabled(enabled & !mCbxMute.isChecked());
        mVsbChanel.setAlpha(mVsbChanel.isEnabled()?1f:DIENABLED_ALPHA);
        mIbtnPlus.setAlpha(enabled?1f:DIENABLED_ALPHA);
        mIbtnSub.setAlpha(enabled?1f:DIENABLED_ALPHA);
        mIbtnPlus.setEnabled(enabled);
        mIbtnSub.setEnabled(enabled);
        mCbxMute.setEnabled(enabled);
        mCbxDegree.setEnabled(enabled);
        mTxtValue.setEnabled(enabled);
    }

    public void setValue(int value, boolean fromUser) {
        this.isFromUser = fromUser;
        setValue(value);
    }

    public void setMute(boolean mute, boolean fromUser) {
        this.isFromUser = fromUser;
        mCbxMute.setChecked(mute);
        this.isFromUser = true;
    }

    public void setDegree(int degree, boolean fromUser) {
        this.isFromUser = fromUser;
        mCbxDegree.setChecked(degree == 1);
        this.isFromUser = true;
    }

    /**
     * 设置选中后的数值
     */
    @SuppressLint("SetTextI18n")
    private void setValue(int value) {
        if (value == 0) {
            mTxtValue.setText("Off");
        } else mTxtValue.setText((-Constants.MAX_VOL + value) + "dB");
        mVsbChanel.setProgress(value);
        isFromUser = true;
    }

    public void setValue(int value, boolean mute, int degree, boolean enabled) {
        setValue(value);
        mCbxMute.setChecked(mute);
        setEnabled(enabled);
        mCbxDegree.setChecked(degree == 1);
    }

    public void setOnSeekBarCallbacks(OnSeekBarCallbacks mOnSeekBarCallbacks) {
        this.mOnSeekBarCallbacks = mOnSeekBarCallbacks;
    }

    @Override
    public void setTag(Object tag) {
        super.setTag(tag);
        mVsbChanel.setTag(tag);
        mCbxMute.setTag(tag);
        mCbxDegree.setTag(tag);
    }

    public int getValue() {
        return mVsbChanel.getProgress();
    }


    public interface OnSeekBarCallbacks {

        void onProgressChanged(SeekBar seekBar, int progress, int currentIndex, boolean fromUser, boolean sendCmd);

        void onProgressChangeOver(SeekBar seekBar, int progress, int currentIndex);

        void onMuteChanged(CompoundButton checkBox, boolean isChecked, int currentIndex);

        void onDegreeChanged(CompoundButton checkBox, boolean isChecked, int currentIndex);
    }
}
